<?php
require_once 'AipOcr.php';
$img_path = "imgs/0.jpg";
if($_POST){
	if($_POST['up_img'] == "up_img"){
		$arr=$_FILES['upfile'];
		if($arr!=''){
			if($arr['error']==0){
				$extarr=array(".jpg",".png");
				$ext=substr($arr['name'],strrpos($arr['name'],"."));
				if(in_array($ext,$extarr)){
					if($arr['size']<5000000){
						$filename = date("H_i_s")." _ ".mt_rand(100, 999).$ext;
//						$path="./up_file/$filename";
						$img_dir = "./imgs/";
						$m = date("m");
						$d = date("d");
						if(!file_exists($img_dir.$m)){
							mkdir($img_dir.$m);
						}
						if(!file_exists($img_dir.$m."/".$d)){
							mkdir($img_dir.$m."/".$d);
						}
						$path = $img_dir.$m."/".$d."/".$filename;
						$re=move_uploaded_file($arr['tmp_name'], $path);
						if($re){
							$img_path = $path;
						}
					}else{
						echo "<script>alert('图片过大！');</script>";
					};
				}else{
					echo "<script>alert('不允许上传的文件格式');</script>";
				};
			}else{
				echo "<script>alert('图片上传错误！请检查是否选中了图片。');</script>";
			};
		}
	}
}
// 你的 APPID AK SK
const APP_ID = '16673201';
const API_KEY = 'r2h1nsSgFc5p3wEcjBLYA8xa';
const SECRET_KEY = 'cFk5N2ZX6AqUAc4TOIlGRScANIbwun61';

$client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

$img = $_GET['img'] ? $_GET['img'] : $img_path;
//$img = "1.png";
//$img = $img.".png";
$image = file_get_contents($img);



// 调用通用文字识别（高进度版）, 图片参数为本地图片

$a = $client->basicAccurate($image);
$arr = $a['words_result'];


//echo "<br><br><br>";
//echo "<hr>";
//echo "-------------------------------------- 以下是专业数据 --------------------------------------------";
//
//echo "<pre>";
//print_r($a);
//echo "</pre>";
?>
<html>
	<head>
		<title>文字识别</title>
		<link rel="stylesheet" type="text/css" href="index.css"/>
	</head>
	<body>

<h3 class="center">左图片，右文字，就这么简单 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- 汪观富</h3>

<div class="main2">
	<div class="frm-div">
		<form class="frm" action="index.php" method="post" enctype="multipart/form-data" >
			<input type="hidden" name="up_img" id="up_img" value="up_img" />
			<input type="file" class="form-control" id="wbl" name="upfile" value="" />
			<input type="submit" class="btn-info" value="上传图片"/>
		</form>
	</div>
</div>

<div class="main">
	<div class="l">
		<img class="l_img" src="<?php echo $img;?>"/>
	</div>
	<div class="zho"></div>
	<div class="r">
<?php
foreach($arr as $v){
	echo $v['words']."<br>";
}
?>
	</div>
	<div class="clear"></div>
</div>


	</body>
</html>